<template>
  <div class="promoWrapper">
    <div class="handle-box">
      <el-date-picker
        v-model="timeVal"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>

      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      <el-button type="primary" icon="edit" @click="create()" class="create-btn">新建开团</el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="startTimestamp" label="开团时间" align="center" :formatter="dateFormat" width="200"></el-table-column>
      <el-table-column prop="endTimestamp" label="结束时间" align="center" :formatter="dateFormat" width="200"></el-table-column>
      <el-table-column prop="isShow" label="前端是否展示" align="center" :formatter="showFormat"></el-table-column>
      <el-table-column prop="categoryNumber" label="商品种类" align="center"></el-table-column>
      <el-table-column prop="managerNumber" label="团长人数" align="center"></el-table-column>
      <el-table-column prop="incomeAmount" label="收入金额" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" align="center" :formatter="statusFormat"></el-table-column>
      <el-table-column label="操作" align="center" width="420">
        <template slot-scope="scope">
          <el-button size="small" @click="handleView(scope.$index, scope.row)">查看</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleOver(scope.$index, scope.row)" v-if="scope.row.status==1">结束活动</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button size="small" @click="handleShow(scope.$index, scope.row)">
            {{scope.row.isShow==0 ? '显示' : '隐藏'}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount" :page-size="paramObj.limit" @current-change="handleCurrentChange">
    </el-pagination>
    
  </div>

</template>

<script>
import api from "@/api/promo";
import { formatDate } from "@/utils/date";

const SUCCESS = 0;
const LIMIT = 10;
export default {
  data() {
    return {
      timeVal: "",
      tableData: [],
      totalCount: 0,
      optId: "",
      paramObj: {
        startTimestamp: null,
        endTimestamp: null,
        currentPage: 1,
        limit: LIMIT
      }
    };
  },
  created() {
    this._getData();
  },
  methods: {
    statusFormat(row, column) {
      let status = row[column.property];
      if (status == 0) {
        return "未开始";
      }

      if (status == 1) {
        return "进行中";
      }

      if (status == 2) {
        return "已结束";
      }
    },
    dateFormat(row, column) {
      let date = row[column.property];
      if (date == undefined) {
        return "";
      }
      date = new Date(date);
      let format = "yyyy-MM-dd hh:mm";
      return formatDate(date, format);
    },
    showFormat(row, column) {
      let isShow = row[column.property];
      if (isShow == 0) {
        return "否";
      } else {
        return "是";
      }
    },
    handleView(index, row) {
      this.$router.push({ path: "/promo/detail", query: { id: row.id } });
    },
    handleOver(index, row) {
      this.optId = row.id;
      this.$confirm("确定现在结束活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._overNow();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleCurrentChange(val) {
      this.paramObj.currentPage = val;
      this._getData();
    },
    handleDelete(index, row) {
      this.optId = row.id;

      this.$confirm("确定现在结束团购?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureDelete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    _sureDelete() {
      api.delete(this.optId).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        }
      });
    },
    _overNow() {
      api.overNow(this.optId).then(response => {
        if (response.code == SUCCESS) {
          //1秒后重新获取数据
          setTimeout(this._getData(), 1000);
          this.$message({
            type: "success",
            message: "操作成功"
          });
        }
      });
    },
    _getData() {
      api.getList(this.paramObj).then(response => {
        if (response.code == SUCCESS) {
          this.tableData = response.data.promoList;
          this.totalCount = response.data.totalCount;
        }
      });
    },
    handleEdit(index, row) {
      this.$router.push({ path: "/promo/edit", query: { id: row.id } });
    },
    create() {
      this.$router.push({ path: "/promo/edit" });
    },
    handleShow(index, row) {
      let isShow;
      if (row.isShow == 0) {
        isShow = 1;
      } else {
        isShow = 0;
      }
      api.setIsShow(row.id, isShow).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "设置成功!"
          });
        }
      });
    },
    search() {
      if (this.timeVal) {
        const format = "yyyy-MM-dd";
        let startTimeStr =
          formatDate(new Date(this.timeVal[0]), format) + " 00:00:00";
        let endTimeStr =
          formatDate(new Date(this.timeVal[1]), format) + " 23:59:59";
        console.log(startTimeStr);
        console.log(endTimeStr);

        this.paramObj.startTimestamp = new Date(startTimeStr).getTime();
        this.paramObj.endTimestamp = new Date(endTimeStr).getTime();
      } else {
        this.paramObj.startTimestamp = null;
        this.paramObj.endTimestamp = null;
      }
      this.paramObj.currentPage = 1;
      this._getData();
    }
  }
};
</script>

<style lang="less">
.promoWrapper {
  .handle-box {
    margin-bottom: 20px;
    .handle-select {
      width: 240px;
    }
    .handle-input {
      width: 200px;
      display: inline-block;
    }
    .create-btn {
      float: right;
      margin-right: 20px;
    }
  }
}
</style>